package com.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pageResult.PageResult;
import com.pojo.Role;
import com.query.QueryPageBean;
import com.result.Result;
import com.service.RoleService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /*删除*/
    @PostMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        roleService.delete(id);
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Role role, Integer [] PermissionIds,Integer [] MenuIds){
        try {
            roleService.update(role,PermissionIds,MenuIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }


    //添加
    @PostMapping("/add")
    public Result add(@RequestBody Role role, Integer [] PermissionIds,Integer [] MenuIds){
        try {
            roleService.add(role,PermissionIds,MenuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_ROLE_FAIL);
    }


    //根据id查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        try {
            Integer[] ids = roleService.queryByPermissionId(id);//权限id
            Integer[] menuId = roleService.queryByMenuId(id);//菜单id
            Role role = roleService.queryById(id);
            Map map=new HashMap();
            map.put("role",role);
            map.put("ids",ids);
            map.put("menuId",menuId);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }




    //查询检查组（分页+条件）
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult result = roleService.selectByQueryString(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    //角色 列表(不支持分页)
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list = roleService.queryAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

}
